package com.tj.test.sys.controller;

import com.tj.test.sys.po.Answer;
import com.tj.test.sys.service.IAnswerService;
import com.tj.test.sys.tool.ResponseMessage;
import com.tj.test.sys.tool.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@CrossOrigin
@RequestMapping("/Answer")
@Api(tags = "Answer")
public class AnswerController {
    @Autowired
    private IAnswerService service;

    @ApiOperation("根据条件查询所有答卷信息")
    @RequestMapping(value = "/selectAllAnswer",method = RequestMethod.GET)
    @ResponseBody
    public ResponseMessage selectAllPaper(Answer answer){
        Map<String,List<Answer>> map=new HashMap<>();
        List<Answer> list=service.selectAllAnswer(answer);
        if (!list.isEmpty()){
            map.put("list",list);
            return Result.success("查询成功",map);
        }
        return Result.error("查询失败",map);
    }

    @ApiOperation("插入答卷信息")
    @RequestMapping(value = "/insertAnswer",method = RequestMethod.GET)
    @ResponseBody
    public ResponseMessage insertAnswer(Answer answer){
        int temp=0;
        if (answer!=null){
            if (answer.getAnswerid()!=0){
                temp=service.insertAnswer(answer);
                if (temp>0) {return Result.success("插入答卷信息成功",temp);}
                else  {return Result.error("该答卷已存在",temp);}
            }
        }
        return Result.error("插入答卷信息失败",temp);
    }

    @ApiOperation("删除答卷信息")
    @RequestMapping(value = "/deleteAnswer",method = RequestMethod.GET)
    @ResponseBody
    public ResponseMessage deleteAnswer(Answer answer){
        int temp=0;
        if (answer!=null){
            if (answer.getAnswerid()!=0){
                temp=service.deleteAnswer(answer);
                if (temp>0) {return Result.success("删除答卷信息成功",temp);}
                else {return Result.error("无该答卷",temp);}
            }
        }
        return Result.error("删除答卷信息失败",temp);
    }

    @ApiOperation("修改答卷信息")
    @RequestMapping(value = "/updatePaper",method = RequestMethod.GET)
    @ResponseBody
    public ResponseMessage updateAnswer(Answer answer){
        int temp=0;
        if (answer!=null){
            if (answer.getAnswerid()!=0){
                temp=service.updateAnswer(answer);
                if (temp>0) {return Result.success("更新答卷信息成功",temp);}
                else {return Result.error("无该答卷表信息",temp);}
            }
        }
        return Result.error("更新答卷信息失败",temp);
    }
}
